/* Color.h
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Header file for the Color class, which represents an RGB color
which can be converted into a pixel value for any SDL surface.
*/

#ifndef __MZ_COLOR_H__
#define __MZ_COLOR_H__

#include "Surface.h"

#include <string>
#include <istream>

using std::string;
using std::istream;



class Color {
public:
	// Default constructor.
	Color();
	// Construct a color from a string, in the form of a hexadecimal number "RRGGBB".
	Color(const string &value);
	// Construct a color with the given RGB value.
	Color(unsigned char red, unsigned char green, unsigned char blue);
	
	// Get the value of this color in the surface that was most recently given to Convert().
	// This allows conversion to be done once and the result to be cached here in Color.
	operator Uint32() const { return converted; }

	// Convert this color to a pixel value in the given surface.
	Uint32 Convert(const Surface &surface) const;
	
	// Read a color from an input stream.
	friend istream &operator>>(istream &in, Color &color);
	

private:
	// Convert a hexadeximal character (0-9 or A-F) into an integer.
	static unsigned char HexToValue(char character);


private:
	// Cached value, the converted color index.
	mutable Uint32 converted;

	// RGB value of this color, plus padding out to a multiple of 4 bytes.
	unsigned char red;
	unsigned char green;
	unsigned char blue;
	unsigned char reserved;
};



#endif

/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
